package com.lai.note.service.impl;

import com.lai.note.dao.NoteMapper;
import com.lai.note.model.Note;
import com.lai.note.service.UserNoteService;
import com.lai.note.util.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class UserNoteServiceImp implements UserNoteService {

    @Autowired
    private NoteMapper noteMapper;

    @Override
    public boolean createNewNote(String userId,String title,String content,String lableId) {
        Note note = new Note();
        note.setNoteId(RandomUtil.getUUID());
        note.setTitle(title);
        note.setContent(content);
        note.setCreateTime(LocalDateTime.now());
        note.setCollectStatus(false);
        System.out.println("即将插入数据库的note:"+note);
        return noteMapper.insertNewNote(note)==1 && noteMapper.relatedUserAndNote(userId,note.getNoteId())==1 && noteMapper.relateLableAndNote(lableId,note.getNoteId())==1;
    }

    @Override
    public boolean updateNote(String noteId, String title,String newContent) {
        Note record = new Note();
        record.setNoteId(noteId);
        record.setTitle(title);
        record.setContent(newContent);
        return noteMapper.updateNote(record)==1;
    }

    @Override
    public boolean deleteNote(String noteId) {
        return noteMapper.deleteNoteById(noteId)==1;
    }

    @Override
    public List<Note> searchNoteByTime(String time) {
        return noteMapper.searchNoteByTime(time);
    }

    @Override
    public List<Note> selectUserAllNotes(String userId) {
        return noteMapper.selectUserAllNotes(userId);
    }
}
